<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;

/**
 * @title 资料下载
 */
class DownloadController extends RestBaseController
{

    /**
     * @title 资料下载列表
     * @description 接口说明
     * @author 开发者
     * @url /api/home/download/downloadList
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:page type:inter require:1 default: other desc:分页页码
     *
     * @return data:资料下载列表@
     * @data id:资料下载id title:资料下载名称 file_url:资料下载地址
     *
     * @return count_page:总页码
     */
    public function downloadList(){
        $language = $this->request->param('language');
        $page = $this->request->param('page');//分页
        //验证
        $rule = config('site.page');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['page'=>$page])) {
            $this->error($validate->getError());
        }

        if(isset($language) && !empty($language)){
            //英文
            $field_download = 'id,title_en title,file_url';
        }else{
            //中文
            $field_download = 'id,title,file_url';
        }
        $limit = CommonController::limit;
        $res = Db::name('download');
        $data = $res->field($field_download)
            ->page($page,$limit)
            ->order('id desc')
            ->select()
            ->toArray();
        foreach ($data as &$value){
            $value['file_url'] = cmf_get_file_download_url($value['file_url']);
        }
        $count = $res->count()/$limit;
        $count_page = ceil($count/$limit);
        $this->success('成功',['data'=>$data,'count_page'=>$count_page]);
    }
}
